| Total Complexity | 2 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { Inject } from '@nestjs/common'; |
||
| 10 | |||
| 11 | @CommandHandler(CreateSchoolCommand) |
||
| 12 | export class CreateSchoolCommandHandler { |
||
| 13 | constructor( |
||
| 14 | @Inject('ISchoolRepository') |
||
| 15 | private readonly schoolRepository: ISchoolRepository, |
||
| 16 | @Inject('IEventBus') |
||
| 17 | private readonly eventBus: IEventBus, |
||
| 18 | private readonly isSchoolAlreadyExist: IsSchoolAlreadyExist |
||
| 19 | ) {} |
||
| 20 | |||
| 21 | public async execute(command: CreateSchoolCommand): Promise<string> { |
||
| 22 | const { |
||
| 23 | reference, |
||
| 24 | address, |
||
| 25 | city, |
||
| 26 | name, |
||
| 27 | zipCode, |
||
| 28 | email, |
||
| 29 | numberOfClasses, |
||
| 30 | numberOfStudents, |
||
| 31 | status, |
||
| 32 | type, |
||
| 33 | observation, |
||
| 34 | phoneNumber |
||
| 35 | } = command; |
||
| 36 | |||
| 37 | if (true === (await this.isSchoolAlreadyExist.isSatisfiedBy(reference))) { |
||
| 38 | throw new SchoolAlreadyExistException(); |
||
| 39 | } |
||
| 40 | |||
| 41 | const school = await this.schoolRepository.save( |
||
| 42 | new School( |
||
| 43 | reference, |
||
| 44 | name, |
||
| 45 | address, |
||
| 46 | zipCode, |
||
| 47 | city, |
||
| 48 | status, |
||
| 49 | type, |
||
| 50 | email, |
||
| 51 | phoneNumber, |
||
| 52 | numberOfStudents, |
||
| 53 | numberOfClasses, |
||
| 54 | observation |
||
| 55 | ) |
||
| 56 | ); |
||
| 57 | |||
| 58 | this.eventBus.publish(new SchoolCreatedEvent(school.getId())); |
||
| 59 | |||
| 60 | return school.getId(); |
||
| 61 | } |
||
| 63 |